<?
/*

SOME DOCUMENTATION:

User and private pages control.
-------------------------------
If there is a private level:
$Engine->page_private_control();

If there's no private level, but there is a user level:
$Engine->user_session();

If you want to force private level:
$Engine->page_private_control( true );

*/



class ROOM extends ENGINE {
	function __construct() {
		// LOAD PAGE DATA
		$this->page_session();
		
		// BUILDING IMPLEMENTED CLASSES
		$this->text = new ROOM_TEXT();
		$this->object = new CORE_OBJECT();
		$this->testuser = new ENGINE_USER(); //!!!
		$this->map = new ROOM_MAP();

	}
	
/** 	PAGE FUNCS
* Primary ROOM subclass content.
* Should be in extended class in the future */

	function page_session() {
		global $Page;
		$Page = $this->page = new PAGE();
		
		if( false && $id = $Page->get( "redirect" ))
			
			$this->page_redirect( $id, false );
		else if(( $type = $Page->get( "objtype" )) && $id = $Page->get( "objid" ))
			
			$this->page_redirect( $id, $type );
	}
	
	function page_private_control( $force = false ) {
		global $Page;
		$this->user_session( $Page->get( "private" ) || $force );
	}
	
	function page_redirect( $id, $is_object ) {
		global $DB;
		
		$Obj = $is_object?
			new OBJECT( $DB->type_r( $is_object ), (int) $id ) : new PAGE((int) $id );
		
		header( "HTTP/1.1 301 Moved Permanently" );
		header( "Status: 301 Moved Permanently" );
		header( "Location: " . $Obj->url() );
		
	}
	
	
/** 	THEME & EXT FUNCTIONALITIES
* Page includes should always come from ./theme level.
* External includes should always come from ./ext 
*
* 	Notice: returns path for inclusion.
*/
	function theme_( $what, $standin = "index.php" ){
		
		return file_exists($file = ROOT . ACCOUNT . "/www" . THEME . $what)? $file : ACCOUNT . "/www" . THEME . $standin;
	}
	
	function ext_( $what ){
		
		if( file_exists( $file = ROOT . ACCOUNT . "/www/ext/" . $what )) return $file;
	}
	
/** 	TEMPLATE & MODULES FUNCTIONALITIES
*
*	Notice: returns actual render.
*/

	function template_( $name, $template_param = false ) {
		if( !file_exists( $template_dir = ROOT . ACCOUNT ."/custom.root/templates/" . $name . ".php") )
			$template_dir = ENGINE_CORE ."templates/" . $name . ".php";

		if( func_num_args() > 2 ) $pre = func_get_arg(2);

		include( $template_dir );
	}
	
	function module_( $identifier, $vars = 0 ) {
		global $Page;
		
		if( $Module = is_int( $identifier )? $Page->mods->call( $identifier ) : $Page->mods->render( $identifier )) {
		
			foreach( $Page->mods->cache as $key => $json )
				if( $key != $Module->modid && json_decode( $json )->mod == $Module->mod ) $exists = 1;
				
			$output = $Module->output( $vars );
			
			if( !$exists ) echo	"\n<script src='/script.mod?mods={$Module->modid}' type='text/javascript'></script>
								 <link rel='stylesheet' href='/style.mod?mods={$Module->modid}' type='text/css' media='screen' />\n";
			
			return $output;
		}
			
	}


	
/** 	OUTPUT FUNCTIONALITIES
* A couple of generic page level output options
* Should be more generic in future. */

}


/** 	TEXT FUNCS
* Get text and conditionals (no set!)
* 											 */

class ROOM_TEXT {
		function __construct() {
			
		}
		
		public function filter( $textid, $column = "short" ) {
			global $DB;
			
			$response = $DB->query( "select distinct lang, `$column` from txtdump where textid=$textid && active" );
			if( $response )
				while( $row = mysql_fetch_array( $response, MYSQL_ASSOC)) $result[ $row[ 'lang' ]] = $row[ $column ];
		
			return $result;
		}
		
		
		public function get( $textid, $column = "short", $lang = LANG, $strict = false ) {
			global $DB;
			
			 $select = ( $strict || engine_get('lang.master') == $lang ) ?
				 "='$lang'" : "IN('$lang','". engine_get('lang.master') ."') ORDER BY lang ".(($lang > engine_get('lang.master'))? "DESC":"");
		
			return utf8_encode( $DB->query_direct( "select `$column` from txtdump where textid = $textid && lang $select limit 1" ));
		}
						
		public function reference( $objtype, $objid, $column = "short", $objcolumn = "nameid" ) {
			global $DB;
			
			if( $objcolumn == "nameid" && $column == "long" ) $objcolumn = "textid";
			
			return $DB->get_key( $objtype, "ID = $objid", "<%lang:$objcolumn:$column%>" );
		}
		
		
		public function search( $string, $column = "short", $lang = LANG ) {
			global $DB;
			
			$response = $DB->query( "select textid, $column from txtdump where $column like '%".$DB->smartq( $string )."%' && lang='$lang'" );
			while( $row[] = mysql_fetch_array( $response, MYSQL_ASSOC));
		
			return $row;
		}
	
}




/** 	SITEMAP FUNCS
* Get site structure conditionals and relations
* Is extention for PAGE class				 */

class ROOM_MAP {
	function __construct() {
		
		
		
	}
		
	public function filter() {
		
		if( !$map = $_SESSION['map']['flat'] ) {
			
			foreach( $_SESSION['map']['menu'] as $node )
				if( $vars = $this->structure( $node['ID'] )) $map[] = $vars;
			
			$_SESSION['map']['flat'] = $map;
		}

		return $map;
	}
	
#	Structure.
#	Return childs of itself, as array
	public function structure( $id ) {
		
		$Page = new PAGE( (int) $id );
		
		if( $Page->visible() ) {
			
			$node = array( "id"=> $id, "name"=> $Page->get( 'name' ), "uri"=> $Page->parse_uri(), "title"=> $Page->get( 'title' ));
			
			if( $subs = $Page->get( 'sub' ))
				foreach( explode( ",", $subs ) as $sub )
					if( $vars = $this->structure( $sub )) $node['subs'][] = $vars;

			return $node;
		}
	}
	
	public function traceRealPage() {
		
		if( $_GET[ "type" ] && $id = $_GET[ "id" ]) {
			global $DB;
			
			return ( $id = $DB->get_key( "page", "<%meta:objid:$id%> && <%meta:objtype:" . $DB->type( $_GET[ "type" ] ) . "%>" ))?
						new PAGE(( int ) $id ) : null;
		}
	}
			
}

/** 	OBJECT FUNCS
* Get/set object conditionals and relations
* Should be in extended class in the future 

! Deprecated, should refer to the OBJECT class */

class CORE_OBJECT {
	function __construct() {
			
	}
		
	public function get( $type, $id ) {
			
		global $DB;
		return $DB->get( $type, "ID=" . $DB->smartq( $id, "int" ) );
	}
			
}







?>